<%@page import="model.Product"%>
<%@page import="model.ProductService"%>
<%@page import="model.SaleOrderDetail"%>
<%@page import="model.SaleOrderDetailService"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="model.SaleOrder"%>
<%@page import="java.util.ArrayList"%>
<%@page import="model.SaleOrderService"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<% 
String SaleOrderID_str = "";
if(((HttpServletRequest) request).getAttribute("SaleOrderID_str") != null) {
    SaleOrderID_str = ((HttpServletRequest) request).getAttribute("SaleOrderID_str").toString();
}
if(((HttpServletRequest) request).getAttribute("error_SaleOrderID") != null) {
    String error_SaleOrderID = ((HttpServletRequest) request).getAttribute("error_SaleOrderID").toString(); %>
    <ul>
        <i style='color:red'><li><%=error_SaleOrderID%></li></i>
    </ul>
<% } %>
<form name="form1" method="post" action="<c:url value="/admin/update_order.do" />">
  <table width="500" border="0">
    <tr>
      <td width="200"><span class="required"></span>Mã Đơn Hàng</td>
      <td width="300"><input type="text" size=20 name="SaleOrderID" value="<%=SaleOrderID_str%>"/></td>
    </tr>
    <tr>
      <td><span class="required"></span>Trạng Thái Đơn Hàng</td>
      <td>
          <select name="Status">
                <option value='Đang Chờ Giao Hàng'>Đang Chờ Giao Hàng</option>
                <option value='Đã Giao Hàng'>Đã Giao Hàng</option>
                <option value='Hủy'>Hủy</option>
        </select>
      </td>
    </tr>
  </table>
  <div style="margin-left: 150px">
    <input type="submit" class="button" name="btnSubmit" value="Tìm Kiếm" />
    <input type="submit" class="button" name="btnSubmit" value="Cập Nhật" >
  </div>
</form>
<hr/>
<% 
if(((HttpServletRequest) request).getAttribute("error_update_status") != null) {
    String error_update_status = ((HttpServletRequest) request).getAttribute("error_update_status").toString(); %>
        <i style='color:red;'><%=error_update_status%></i><br/><br/>
<% } %>
<% 
    ArrayList<SaleOrder> lstSaleOrder = new ArrayList<SaleOrder>();
    if(((HttpServletRequest) request).getAttribute("lstSaleOrders") != null) {
        lstSaleOrder = (ArrayList<SaleOrder>) ((HttpServletRequest) request).getAttribute("lstSaleOrders");
    } else {
        SaleOrderService sale_order_service = new SaleOrderService();
        lstSaleOrder = sale_order_service.getSaleOrders();
    }
    if(((HttpServletRequest) request).getAttribute("error_lstSaleOrder") != null) { 
        String error_lstSaleOrder = ((HttpServletRequest) request).getAttribute("error_lstSaleOrder").toString(); %>
        <i style='color:red;'><%=error_lstSaleOrder%></i>
<%    } else { %>
<table id="cart" border=1 cellspacing=0 cellpadding=4 bordercolor=lightgray width="100%">
    <tr bgcolor=#dadddd>
            <th>Mã Đơn Hàng</th>
            <th>Loại KH</th>
            <th>Tên KH</th>
            <th>Địa Chỉ</th>
            <th>Số Điện Thoại</th>
            <th>Ngày Đặt Hàng</th>
            <th>Tổng Tiền (đ)</th>
            <th>Cách Thanh Toán</th>
            <th>Trạng Thái</th>
    </tr>
<%    int j = 0;
    for(int i = 0; i < lstSaleOrder.size(); i++) {
        String color = ++j % 2 == 0 ? "#f9e7dd" : "#eec7b7";
        int SaleOrderID = lstSaleOrder.get(i).getSaleOrderID();
        int MemberID = lstSaleOrder.get(i).getMemberID();
        String Member_str = "Thường";
        if(MemberID > 0) {
            Member_str = "Thành Viên";
        }
        String CustomerName = lstSaleOrder.get(i).getCustomerName();
        String Address = lstSaleOrder.get(i).getShippingAddress();
        String Phone = lstSaleOrder.get(i).getShippingPhone();
        Timestamp OrderDate = lstSaleOrder.get(i).getOrderDate();
        String date_str = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(OrderDate);
        double TotalPrice = lstSaleOrder.get(i).getTotalPrice();
        String Status = lstSaleOrder.get(i).getStatus();
        String PaymentType = lstSaleOrder.get(i).getPaymentType();
%>
    <tr bgcolor="<%=color%>">
        <td align=center><%=SaleOrderID%></td>
        <td align=center><%=Member_str%></td>
        <td align=center><%=CustomerName%></td>
        <td align=center><%=Address%></td>
        <td align=center><%=Phone%></td>
        <td align=center><%=date_str%></td>
        <td align=right><fmt:formatNumber type="currency" currencySymbol="" maxFractionDigits="0" value="<%=TotalPrice%>"/></td>
        <td align=center><%=PaymentType%></td>
        <td align=center style="color:red;"><%=Status%></td>
    </tr>
    <tr bgcolor=#dadddd>
        <td colspan="9">
            <table id="cart" border=1 cellspacing=0 cellpadding=4 bordercolor=lightgray width="650px" style="margin-left: 15px; margin-bottom: 15px;">
                <tr bgcolor=#b7b8b8>
                    <th>#</th>
                    <th>Mã Sản Phẩm</th>
                    <th>Tên Sản Phẩm</th>
                    <th align=center>Đơn Giá (đ)</th>
                    <th>Số Lượng</th>
                    <th>Thành Tiền (đ)</th>
                </tr>
<% 
    SaleOrderDetailService sale_order_detail_service = new SaleOrderDetailService();
    ArrayList<SaleOrderDetail> lstSaleOrderDetails = sale_order_detail_service.getSaleOrderDetails_bySaleOrderID(SaleOrderID);
    int k = 0;
    for(int m = 0; m < lstSaleOrderDetails.size(); m++) {
        String color_table2 = ++k % 2 == 0 ? "#f8ad9a" : "#f4c3b6";      
        String pid = lstSaleOrderDetails.get(m).getProductID();
        int qty = lstSaleOrderDetails.get(m).getOrderQuantity();
        double totallinefmt = lstSaleOrderDetails.get(m).getLineTotal();
        ProductService product_service = new ProductService();
        Product product = product_service.getProductDetails_byPID(pid);
        String pname = product.getProductName();
        double unitpricefmt = product.getUnitPrice();       
%>
                <tr bgcolor="<%=color_table2%>">
                        <td align=center><%=k%></td>
                        <td align=center><%=pid%></td>		
                        <td align=center><%=pname%></td>
                        <td align=right><fmt:formatNumber type="currency" currencySymbol="" maxFractionDigits="0" value="<%=unitpricefmt%>"/></td>
                        <td align=center><%=qty%></td>
                        <td align=right><fmt:formatNumber type="currency" currencySymbol="" maxFractionDigits="0" value="<%=totallinefmt%>"/></td>
                </tr>

<% } %>
            </table>
        </td>
    </tr>
<% } } %>
</table>
